/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrn.projetoDB.gerenciadorGaragem.persistencia;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Criterion;

import br.ufrn.projetoDB.gerenciadorGaragem.persistencia.util.HibernateUtil;


/**
 *
 * @author taeko
 */
public abstract class DAOGenericoHibernate<T> implements IDAOGenerico<T> {

    private Class<T> persistentClass;
    private Session session;

    public DAOGenericoHibernate() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }

    @SuppressWarnings("unchecked")
    public void setSession(Session s) {
        this.session = s;
    }

    public Session getSession() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        return session;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    @SuppressWarnings("unchecked")
    public abstract T findById(Integer id);

    @SuppressWarnings("unchecked")
    public abstract List<T> findAll();

    @SuppressWarnings("unchecked")
    public abstract List<T> findByExample(T exampleInstance, String[] excludeProperty);

    public void flush() {
        getSession().flush();
    }

    public void clear() {
        getSession().clear();
    }

    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected abstract List<T> findByCriteria(Criterion... criterion);

    public abstract void addEntity(T entity);

    public abstract void updateEntity(T entity);

    public abstract void removeEntity(T entity);

}
